import java.io.File;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LianBao
 * Date: 2024-08-15
 * Time: 14:52
 */

//案例1:扫描指定目录,找到名称包含指定字符的所有普通文件(不包括目录),并且询问是否要删除该文件
//


public class demo11 {
    private static void scan(File cur, String key) {
        if (!cur.isDirectory()) {
            return;
        }
        File[] files = cur.listFiles();//列出所有内容
        if (files.length == 0 || files == null) {
            return;
        }
        for (int i = 0; i < files.length; i++) {
            if (files[i].isFile()) {
                //普通文件,删除操作
                delete(files[i], key);
            } else {
                scan(files[i], key);//进行递归
            }
        }
    }

    private static void delete(File file, String key) {
        if (!file.getName().contains(key)) {
            return;//不包含关键字
        }
        Scanner scanner = new Scanner(System.in);
        System.out.println("是否要进行删除文件:" + file.getAbsolutePath() + "  Y表示是");
        String input = scanner.next();
        if (input.equals("Y")) {
            file.delete();
        }
    }

    public static void main(String[] args) {
        System.out.println("输入搜索的路径");
        Scanner scanner = new Scanner(System.in);
        String rootPath = scanner.next();
        File file = new File(rootPath);
        if (!file.isDirectory()) {
            System.out.println("路径不存在");
            return;
        }
        System.out.println("输入关键字");
        String key = scanner.next();
        scan(file, key);
    }
}
